package com.atguigu.service.impl;

import com.atguigu.entity.PlatformPropertyKey;
import com.atguigu.entity.PlatformPropertyValue;
import com.atguigu.mapper.PlatformPropertyKeyMapper;
import com.atguigu.service.PlatformPropertyKeyService;
import com.atguigu.service.PlatformPropertyValueService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 属性表 服务实现类
 * </p>
 *
 * @author zhangqiang
 * @since 2021-08-21
 */
@Service
public class PlatformPropertyKeyServiceImpl extends ServiceImpl<PlatformPropertyKeyMapper, PlatformPropertyKey> implements PlatformPropertyKeyService {
    @Autowired
    private PlatformPropertyValueService propertyValueService;


    @Override
    public List<PlatformPropertyKey> getPlatformPropertyByCategoryId(Long category1Id, Long category2Id, Long category3Id) {
        //第一种方式性能不高
        //b.先根据分类id查询平台属性key 先写SQL
//        List<PlatformPropertyKey> platformPropertyKeyList=baseMapper.getPropertyKeyByCategoryId(category1Id,category2Id,category3Id);
//        //c.再根据平台属性key的id查询平台属性值
//        for (PlatformPropertyKey platformPropertyKey : platformPropertyKeyList) {
//           List<PlatformPropertyValue> propertyValueList= propertyValueService.getPropertyValueByPropertyKeyId(platformPropertyKey.getId());
//           platformPropertyKey.setPropertyValueList(propertyValueList);
//        }
        //第二种方式 推荐
        List<PlatformPropertyKey> platformPropertyKeyList= baseMapper.getPlatformPropertyByCategoryId(category1Id,category2Id,category3Id);
        return platformPropertyKeyList;
    }

    @Override
    public void savePlatformProperty(PlatformPropertyKey platformPropertyKey) {
        //判断平台属性里面是否有id 如果有就是修改
        if(platformPropertyKey.getId()!=null){
            baseMapper.updateById(platformPropertyKey);
        }else{
            //1.保存数据到平台属性key categoryId propertyKey
            baseMapper.insert(platformPropertyKey);
        }

        //平台属性value 把原来的先删除 然后再重新添加
        QueryWrapper<PlatformPropertyValue> wrapper = new QueryWrapper<>();
        wrapper.eq("property_key_id",platformPropertyKey.getId());
        propertyValueService.remove(wrapper);

        //2.保存数据到平台属性value
        List<PlatformPropertyValue> propertyValueList = platformPropertyKey.getPropertyValueList();
        //先判断propertyValueList是否为空
        if(propertyValueList!=null){
            for (PlatformPropertyValue platformPropertyValue : propertyValueList) {
                //此时platformPropertyKey.getId()能不能拿到id的值? 有 如果不是mybatis plus 没有id
                platformPropertyValue.setPropertyKeyId(platformPropertyKey.getId());
                //方式一
                //propertyValueService.save(platformPropertyValue);
            }
            //方式二 批处理
            propertyValueService.saveBatch(propertyValueList);
        }
    }

    @Override
    public List<PlatformPropertyKey> getPlatformPropertyBySkuId(Long skuId) {
        return baseMapper.getPlatformPropertyBySkuId(skuId);
    }
}
